package main

/*
*

	<https://www.cnblogs.com/hiddenfox/p/3408931.html>
	![](http://cdn.bsummer.cn/20200408020004.png)
*/

import . "local/algorithm/util"

func detectCycle(head *ListNode) *ListNode {
	if head == nil {
		return nil
	}

	i, j := head, head
	for j != nil && j.Next != nil {
		i = i.Next
		j = j.Next.Next
		if i == j {
			i = head
			for i != j {
				i = i.Next
				j = j.Next
			}
			return i
		}
	}
	return nil
}
